package com.lingdiman.ocr.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.lingdiman.ocr.R;

import java.util.List;

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.Holder> {

    private List<Integer> mDatas;

    public void setData(List<Integer> data) {
        mDatas = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, null);
        //自定义view的宽度，控制一屏显示个数
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        int width = parent.getContext().getResources().getDisplayMetrics().widthPixels;
        int itemWidth = (int) (width / 2);
        params.width = itemWidth;
        params.height = itemWidth * 2444 / 1080;
        view.setLayoutParams(params);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.iv.setImageResource(mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        private ImageView iv;

        public Holder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.item_iv);
        }
    }
}
